package sg.edu.nus.iss.phoenix.scheduledprogram.controller;

import java.io.IOException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.DispatcherType;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import sg.edu.nus.iss.phoenix.utils.*;
import sg.edu.nus.iss.phoenix.core.controller.FCUtilities;
import sg.edu.nus.iss.phoenix.schedule.entity.AnnualSchedule;
import sg.edu.nus.iss.phoenix.schedule.entity.ProgramSlot;
import sg.edu.nus.iss.phoenix.schedule.entity.WeeklySchedule;
import sg.edu.nus.iss.phoenix.scheduledprogram.delegate.ReviewSelectScheduledProgramDelegate;

/**
 * Servlet implementation class ScheduledProgramController
 * @author Wang Shunde
 */
@WebServlet("/ScheduledProgramController/*")
public class ScheduledProgramController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ReviewSelectScheduledProgramDelegate reviewSelectScheduledProgramDelegate;
	private List<WeeklySchedule> selectedWeeklySchedules;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ScheduledProgramController() {
        super();
        reviewSelectScheduledProgramDelegate = new ReviewSelectScheduledProgramDelegate();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void processRequest(HttpServletRequest request,  HttpServletResponse response) throws ServletException, IOException {
		String pathInfo = request.getPathInfo();
		String action = FCUtilities.stripPath(pathInfo);

		String forwardTo= "/pages/reviewScheduledProgram.jsp";
		
		switch(action){
		
		case "searchAnnualSchedule":
			List<AnnualSchedule> annualSchedules = reviewSelectScheduledProgramDelegate.searchAllAnnualSchedule();
			request.setAttribute("annualSchedules", annualSchedules);
			break;
			
		case "searchWeeklySchedule":
			int annualScheduleId = Integer.parseInt(request.getParameter("annualScheduleId"));
			selectedWeeklySchedules = reviewSelectScheduledProgramDelegate.searchWeeklySchedule(annualScheduleId);
			request.setAttribute("weeklySchedules", selectedWeeklySchedules);
			break;
			
		case "searchProgramSlot":
			long weeklyScheduleId = Long.parseLong(request.getParameter("weeklyScheduleId"));
			WeeklySchedule selected = getSelected(weeklyScheduleId);
			List<ProgramSlot> programSlots = reviewSelectScheduledProgramDelegate.searchProgramSlot(weeklyScheduleId);
			selected.setProgramSlotsforWeek((ArrayList<ProgramSlot>)programSlots);
			request.getSession().setAttribute("weeklySchedule", selected);
			request.getSession().setAttribute("programSlotsforWeek", programSlots);
			break;
			
		case "showScheduledProgramPage":
			break;
			
			
			default:
				forwardTo= Constants.ERROR_PAGE;
		}
		RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(forwardTo);
		dispatcher.forward(request, response);
		
	}
	
	private WeeklySchedule getSelected(long id) {
		for(int i=0; i<selectedWeeklySchedules.size(); i++){
			if(selectedWeeklySchedules.get(i).getId() == id)
				return selectedWeeklySchedules.get(i);
		}
		return null;
	}
}
